﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Xml;
namespace ProductManagement
{
    public class ProductManager
    {
        public class DataSetHelper
        {
            public DataSet ds;

            public DataSetHelper(DataSet  DataSet)
            {
                ds = DataSet;
            }

            public DataSetHelper()
            {
                ds = null;
                }

        }

        public DataTable GetSelectedProduct(int productid )
        {

            var dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("NAME");
            dt.Columns.Add("TYPE");
            dt.Columns.Add("QUANTITY");
            dt.Columns.Add("UNIT");
            dt.Columns.Add("EXPIRE_DATE");

            DataRow dr = dt.NewRow();

            var doc  = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));
            
            var nodeSelect = doc.SelectSingleNode("descendant::Product[./ID/text()='" + productid + "']");
            if(nodeSelect !=null)
            {
            dr["ID"] = nodeSelect.ChildNodes[0].InnerText;
            dr["NAME"] = nodeSelect.ChildNodes[1].InnerText;
            dr["TYPE"] = nodeSelect.ChildNodes[2].InnerText;
            dr["QUANTITY"] = nodeSelect.ChildNodes[3].InnerText;
            dr["UNIT"] = nodeSelect.ChildNodes[4].InnerText;
            dr["EXPIRE_DATE"] = nodeSelect.ChildNodes[5].InnerText;

            dt.Rows.Add(dr);

            return dt;}
            return null;

            

        }


        //public DataTable GetProductList(int category  , int priority , string orderCol ,int startRowIndex, int maximumRows) 
        public DataTable GetProductList(int startRowIndex, int maximumRows) 
        {

            DataSetHelper dsHelper;

            var ds = new DataSet();
            dsHelper = new DataSetHelper(ds);
            //ds.ReadXml(Server.MapPath("~/App_Code/ProductManager.xml"));

            ds.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));

            var dtProduct  = ds.Tables["Product"];
            var dtUnit  = ds.Tables["Unit"];
            var dtTypeProduct = ds.Tables["Types"];

            ds.Relations.Add("ProductUnit", dtUnit.Columns["ID"], dtProduct.Columns["UNIT"]);

            var dtJt  = new DataTable("ProductList");
            dtJt.Columns.Add("ID", Type.GetType("System.Int32"));
            dtJt.Columns.Add("NAME", Type.GetType("System.String"));
            dtJt.Columns.Add("TYPE", Type.GetType("System.String"));
            dtJt.Columns.Add("QUANTITY", Type.GetType("System.Int32"));
            dtJt.Columns.Add("UNIT", Type.GetType("System.String"));
            dtJt.Columns.Add("EXPIRE_DATE", Type.GetType("System.String"));
            dtJt.Columns.Add("UNITNAME", Type.GetType("System.String"));

            ds.Tables.Add(dtJt);

            foreach(DataRow dr in ds.Tables["Product"].Rows)
            {
                var parent  = dr.GetParentRow("ProductUnit");
                var current = dtJt.NewRow();
                for(var i  = 0 ;i<ds.Tables["Product"].Columns.Count ;i++)
                    current[i]= dr[i];
                

                current["UNITNAME"] = parent["NAME"];
                dtJt.Rows.Add(current);
            }
            return ds.Tables["ProductList"];
            

        }

        public int GetProductCount(int startRowIndex, int maximumRows)
        {
            return 100;
        }

        public DataTable GetProductType()
        {
            var ds = new DataSet();
            ds.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));
            var dtProductType = ds.Tables["PRODUCTTYPE"];
            return dtProductType;
        }

        public DataTable GetUnitType()
        {
            var ds = new DataSet();
            ds.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));
            var dtUnitType = ds.Tables["Unit"];
            return dtUnitType;
        }

        public bool UpdateProduct(int ID, string name, int type, int quantity, int unit, string expiredate)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));
            XmlNode node = doc.SelectSingleNode("Products/Product[@ID='" + ID + "']");
            if (node != null)
            {
                node.ChildNodes[0].InnerText = ID.ToString();
                node.ChildNodes[1].InnerText = name;
                node.ChildNodes[2].InnerText = type.ToString();
                node.ChildNodes[3].InnerText = quantity.ToString();
                node.ChildNodes[4].InnerText = unit.ToString();
                node.ChildNodes[5].InnerText = expiredate;
                doc.Save(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));
                return true;
            }
            return false;
        }

        public bool DeleteProduct(int id)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));
            XmlNode node = doc.SelectSingleNode("Products/Product[@ID='" + id + "']");
            if (node != null)
            {
                doc.DocumentElement.RemoveChild(node);
                doc.Save(HttpContext.Current.Server.MapPath("~/App_Data/ProductManager.xml"));
                return true;
            }
            

            return false;
        }

    }
}